// SPDX-FileCopyrightText: 2023-2025 Sangfor Technologies Inc.
// SPDX-License-Identifier: Mulan PSL v2
package com.geniusai.aip.auth.controller;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

/**
 * @author longping58607
 * @date 2020/12/14
 */
@RestController
@Validated
public class KaptchaController {

    /**
     * 验证码工具
     */
    @Autowired
    DefaultKaptcha defaultKaptcha;

    @GetMapping("/v1/kaptcha")
    public void defaultKaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 设置内容类型
        response.setContentType("image/jpeg");
        // 创建验证码文本
        String createText = defaultKaptcha.createText();
        // 将生成的验证码保存在session中
        request.getSession().setAttribute("code", createText);
        request.getSession().setAttribute("date", System.currentTimeMillis());
        // 创建验证码图片
        BufferedImage bi = defaultKaptcha.createImage(createText);

        // 获取响应输出流
        ServletOutputStream out = response.getOutputStream();
        // 将图片验证码数据写入到图片输出流
        ImageIO.write(bi, "jpg", out);

        // 推送并关闭输出流
        out.flush();
        out.close();

    }

}
